package View;
import Service.HouseService;
import domain.House;

import java.util.Scanner;
public class View {
    Scanner scanner=new Scanner(System.in);
    private boolean loop=true;
    private int key;
    private HouseService houseServices=new HouseService(10);
private void update(){
    System.out.println("==========修改信息======");
    System.out.println("请选择要修改的编号（-1退出）");
    int choice=scanner.nextInt();
    scanner.nextLine();
    if(choice==-1){
        return;
    }
    House house=houseServices.getHouses()[choice-1];
    System.out.println("姓名("+house.getName()+"):");
    house.setName(scanner.nextLine());
    System.out.println("电话("+house.getPhone()+"):");
    house.setPhone(scanner.nextLine());
    System.out.println("地址("+house.getAddress()+"):");
    house.setAddress(scanner.nextLine());
    System.out.println("月租(" + house.getRent() + "):");
    String rentInput = scanner.nextLine();
    if (!rentInput.isEmpty()) {
        house.setRent(Integer.parseInt(rentInput));
    }
    System.out.println("状态("+house.getState()+"):");
    house.setState(scanner.nextLine());
    mainmenu();
}
    private void findHouse(){
        System.out.println("========查找房屋=========");
        System.out.println("请输入你要找的id");
        int choice=scanner.nextInt();
        scanner.nextLine();
       if( houseServices.find(choice)){

       }else {
           System.out.println("不存在");
       }
    }
    public void subHouse(){
        boolean choice=false;
        System.out.println("请选择删除房屋的编号（-1退出）");
        do{
        int id=scanner.nextInt();
        scanner.nextLine();
        if(id<-1||id==0||id>houseServices.getId()){
            System.out.println("无该编号的房屋");
            System.out.println("请重新输入");
            choice=true;
            continue;
        }else if(id==-1){
            System.out.println("成功退出");
            return;
        }
        System.out.println("确定删除吗？y/n:");
        String yorn=scanner.nextLine();
        if(yorn.equals("y")){
            houseServices.sub(id);
            System.out.println("成功删除");
        }
        }while (choice);
    }
    public void addHouse(){
        System.out.println("========添加房屋========");
        System.out.println("姓名:");
        String name=scanner.nextLine();
        System.out.println("电话：");
        String phone=scanner.nextLine();
        System.out.println("地址");
        String address=scanner.nextLine();
        System.out.println("月租");
        int rent=scanner.nextInt();
        scanner.nextLine();
        System.out.println("状态");
        String state=scanner.nextLine();
        House house=new House(houseServices.getId(),name,phone,address,rent,state);
        //将创建的房屋添加到数组
        if(houseServices.add(house))
        {
            System.out.println("成功添加房屋");
        }
        else {
            System.out.println("添加失败");
        }
    }
    private void listHouse(){
        System.out.println("=======房屋列表==========");
        System.out.println("编号  房主  电话  地址  月租  状态（未出租/已出租）");
        House[] houses=houseServices.list();
        for (int i = 0; i < houses.length; i++) {
            if(houses[i]==null){
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("======显示完毕======");
    }
    public void mainmenu(){
        do{
            System.out.println("===========房屋出租系统菜单========");
            System.out.println("   1.新增房源");
            System.out.println("   2.查找房屋");
            System.out.println("   3.删除房屋信息");
            System.out.println("   4.修改房屋信息");
            System.out.println("   5.房屋列表");
            System.out.println("   6.退出系统");
            System.out.println("请选择：");
            key=scanner.nextInt();
            scanner.nextLine();
            switch (key){
                case 1:
                    addHouse();
                    break;
                case 2:
                    findHouse();
                    break;
                case 3:
                    subHouse();
                    break;
                case 4:
                    update();
                    break;
                case 5:
                    listHouse();
                    break;
                case 6:
                    System.out.println("退出");
                    loop=false;
                    break;
            }
        }while (loop);
    }
}
